﻿using System;
using System.ServiceModel;
using System.ServiceModel.Activation;
using MugenInjection.Parameters;
using MugenInjection.Web;

namespace MugenInjection.Wcf
{
    /// <summary>
    /// Base class for the host factory for the specified <see cref="ServiceHost"/>.
    /// </summary>
    public abstract class MugenInjectionServiceHostFactoryBase : ServiceHostFactory
    {
        #region Abstract or virtual

        /// <summary>
        /// Gets the specified <see cref="ServiceHost"/> type.
        /// </summary>
        /// <param name="serviceType">Specifies the type of service to host. </param>
        /// <returns></returns>
        protected abstract Type GetServiceHostType(Type serviceType);

        #endregion

        #region Overrides of ServiceHostFactory

        /// <summary>
        /// Creates a <see cref="T:System.ServiceModel.ServiceHost"/> for a specified type of service with a specific base address. 
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.ServiceModel.ServiceHost"/> for the type of service specified with a specific base address.
        /// </returns>
        /// <param name="serviceType">Specifies the type of service to host. </param><param name="baseAddresses">The <see cref="T:System.Array"/> of type <see cref="T:System.Uri"/> that contains the base addresses for the service hosted.</param>
        protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
        {
            var typeParameter = new ConstructorParameter("serviceType", typeof(Type), serviceType);
            var addressesParameter = new ConstructorParameter("baseAddresses", typeof(Uri[]), baseAddresses);
            return (ServiceHost)MugenInjectionHttpApplication
                                     .ApplicationConfiguration
                                     .Injector.Get(GetServiceHostType(serviceType), typeParameter, addressesParameter);
        }

        #endregion
    }
}